package news.fragment;

import android.content.Context;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;

import com.jeremyfeinstein.slidingmenu.lib.SlidingMenu;



import java.util.ArrayList;
import java.util.List;
import base.BaseFragment;
import base.BasePage;
import home.FunctionPage;
import home.GovAffairsPage;
import home.NewsCenterPage;
import home.SettingPage;
import home.SmartServiecePage;
import shanshuitiancheng.news.R;
import view.CustomViewPager;


/**
 * Created by cch on 2016/6/13.
 */
public class HomeFragment extends BaseFragment {
    private final String TAG = "我点击了";
    private View view;
    //依赖注入框架利用反射原理将控件和事件绑定，节省你诸如 f
    // indViewById 这样的代码，写起来方便 。 优点： 开发速度快，
    // 缺点： 对性能消耗略大。google官方不甚推荐。
//    @ViewInject(R.id.viewpager)
    private CustomViewPager viewpager;
//    @ViewInject(R.id.main_radio)
    private RadioGroup main_radio;
//    private int checkedId;  普通初始化
    private int checkedId = R.id.rb_function;//初始化点击第一个rb_function

    /*    @Override
            public void onActivityCreated(Bundle savedInstanceState) {
                super.onActivityCreated(savedInstanceState);
            }

            @Override
            public void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
            }

            @Nullable
            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //        LayoutInflater inflater1 = (LayoutInflater)getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //        View view = LayoutInflater.from(getActivity()).inflate(R.layout.frag_home2,null);
                View view = inflater.inflate(R.layout.frag_home2,null);
                return view;
            }*/
/*
* 1·初始化viewpager 通过使用adapter的形式去实现
* */
    @Override
//    初始化view
    public View initView(LayoutInflater inflater) {
                view = inflater.inflate(R.layout.frag_home2, null);//加载要初始化的布局文件
        viewpager = (CustomViewPager) view.findViewById(R.id.viewpager);//初始化化布局文件里面的viewpager
        main_radio = (RadioGroup) view.findViewById(R.id.main_radio);//初始化布局文件RadioGroup里面的控件

       // ViewUtils.inject(this, view);//注入view和事件
        return view;
    }

    List<BasePage> list = new ArrayList<BasePage>();//创建一个数组适配器来装载相关数据
    @Override
//    初始化RADIOGroup里面的数据
        public void initData(Bundle savedInstanceState) {
        list.add(new FunctionPage(ct));
        list.add(new NewsCenterPage(ct));
        list.add(new SmartServiecePage(ct));
        list.add(new GovAffairsPage(ct));
        list.add(new SettingPage(ct));


        HomePageAdapter adapter = new HomePageAdapter(ct,list);
        viewpager.setAdapter(adapter);
        viewpager.setOnPageChangeListener(new CustomViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
//如果位置是0的话，才能出现滑动菜单。。如果是其他的tab出现的时候，滑动菜单就给屏蔽掉
                if (1 == position) {
                    sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
                } else {
                    sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
            }

            BasePage page = list.get(position);
            page.initData();
        }

        @Override
        public void onPageScrollStateChanged ( int state){

        }
        });

        main_radio.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()

    {
        @Override
        public void onCheckedChanged (RadioGroup group,int checkedId){
        switch (checkedId) {
            case R.id.rb_function:
                viewpager.setCurrentItem(0, false);
                checkedId = 0;
                Log.i(TAG, "第一个键" + checkedId);
                break;

            case R.id.rb_news_center:
                viewpager.setCurrentItem(1, false);
                checkedId = 1;
                Log.i(TAG, "第二个键" + checkedId);
                break;
            case R.id.rb_smart_service:
                viewpager.setCurrentItem(2, false);
                checkedId = 2;
                Log.i(TAG, "第三个键" + checkedId);
                break;
            case R.id.rb_gov_affairs:
                viewpager.setCurrentItem(3, false);
                checkedId = 3;
                Log.i(TAG, "第四个键" + checkedId);
                break;
            case R.id.rb_setting:
                viewpager.setCurrentItem(4, false);
                checkedId = 4;
                Log.i(TAG, "第五个键" + checkedId);
                break;
        }
    }
        });
    main_radio.check(checkedId);
    }

     class HomePageAdapter extends PagerAdapter {
         private Context ct;
         private List<BasePage> list;
         public HomePageAdapter(Context ct, List<BasePage> list) {
             this.ct = ct;
             this.list = list;
         }

         @Override
         public int getCount() {
             return list.size();
         }

         @Override
         public boolean isViewFromObject(View arg0, Object arg1) {
             return arg0 == arg1;
         }

         @Override
         public void destroyItem(ViewGroup container, int position, Object object) {
//             当点击下一个视图时他会销毁当前的视图
//             super.destroyItem(container, position, object);
             ((CustomViewPager)container).removeView(list.get(position).getRootView());
         }

         @Override
         public Object instantiateItem(ViewGroup container, int position) {
//             当点击下一个视图时 他会呈现下一个视图
             ((CustomViewPager)container).addView(list.get(position).getRootView(),0);
             return list.get(position).getRootView();
         }
     }
}
